css: Handle custom properties in a custom object
authorBenjamin Otte <otte@redhat.com>
Sat, 31 Dec 2011 22:04:32 +0000 (23:04 +0100)
committerBenjamin Otte <otte@redhat.com>
Mon, 9 Jan 2012 17:37:53 +0000 (18:37 +0100)
This way we can also get rid of the hack where we required modifying the
pspec after creation, as the name is now a separate property.

gtk/Makefile.am
gtk/gtkcsscustomproperty.c [new file with mode: 0644]
gtk/gtkcsscustompropertyprivate.h [new file with mode: 0644]
gtk/gtkstyleproperties.c
gtk/gtkstyleproperties.h
gtk/gtkthemingengine.c
gtk/gtkthemingengine.h

index 13252b66364b6bf8d1faa0e061e2256be2db9a5a..0dd8e768b0cc0e8b3092d343f2f6b8b6eab1be62 100644 (file)
@@ -411,6 +411,7 @@ gtk_private_h_sources =             \
        gtkbuttonprivate.h      \
        gtkcellareaboxcontextprivate.h  \
        gtkcontainerprivate.h   \
+       gtkcsscustompropertyprivate.h \
        gtkcsslookupprivate.h   \
        gtkcssparserprivate.h   \
        gtkcssproviderprivate.h \
@@ -586,6 +587,7 @@ gtk_base_c_sources =                \
        gtkcombobox.c           \
        gtkcomboboxtext.c       \
        gtkcontainer.c          \
+       gtkcsscustomproperty.c  \
        gtkcsslookup.c          \
        gtkcssparser.c          \
        gtkcssprovider.c        \
diff --git a/gtk/gtkcsscustomproperty.c b/gtk/gtkcsscustomproperty.c
new file mode 100644 (file)
index 0000000..1d49492
--- /dev/null
@@ -0,0 +1,209 @@
+/*
+ * Copyright © 2011 Red Hat Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors: Benjamin Otte <otte@gnome.org>
+ */
+
+#include "config.h"
+
+#include "gtkcsscustompropertyprivate.h"
+
+#include <string.h>
+
+#include "gtkthemingengine.h"
+
+G_DEFINE_TYPE (GtkCssCustomProperty, _gtk_css_custom_property, GTK_TYPE_CSS_STYLE_PROPERTY)
+
+static void
+_gtk_css_custom_property_class_init (GtkCssCustomPropertyClass *klass)
+{
+}
+
+
+static void
+_gtk_css_custom_property_init (GtkCssCustomProperty *custom_property)
+{
+}
+
+static void
+gtk_css_custom_property_create_initial_value (GParamSpec *pspec,
+                                              GValue     *value)
+{
+  g_value_init (value, pspec->value_type);
+
+  if (pspec->value_type == GTK_TYPE_THEMING_ENGINE)
+    g_value_set_object (value, gtk_theming_engine_load (NULL));
+  else if (pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
+    g_value_take_boxed (value, pango_font_description_from_string ("Sans 10"));
+  else if (pspec->value_type == GDK_TYPE_RGBA)
+    {
+      GdkRGBA color;
+      gdk_rgba_parse (&color, "pink");
+      g_value_set_boxed (value, &color);
+    }
+  else if (pspec->value_type == GTK_TYPE_BORDER)
+    {
+      g_value_take_boxed (value, gtk_border_new ());
+    }
+  else
+    g_param_value_set_default (pspec, value);
+}
+
+/* Property registration functions */
+
+/**
+ * gtk_theming_engine_register_property: (skip)
+ * @name_space: namespace for the property name
+ * @parse_func: parsing function to use, or %NULL
+ * @pspec: the #GParamSpec for the new property
+ *
+ * Registers a property so it can be used in the CSS file format,
+ * on the CSS file the property will look like
+ * "-${@name_space}-${property_name}". being
+ * ${property_name} the given to @pspec. @name_space will usually
+ * be the theme engine name.
+ *
+ * For any type a @parse_func may be provided, being this function
+ * used for turning any property value (between ':' and ';') in
+ * CSS to the #GValue needed. For basic types there is already
+ * builtin parsing support, so %NULL may be provided for these
+ * cases.
+ *
+ * <note>
+ * Engines must ensure property registration happens exactly once,
+ * usually GTK+ deals with theming engines as singletons, so this
+ * should be guaranteed to happen once, but bear this in mind
+ * when creating #GtkThemeEngine<!-- -->s yourself.
+ * </note>
+ *
+ * <note>
+ * In order to make use of the custom registered properties in
+ * the CSS file, make sure the engine is loaded first by specifying
+ * the engine property, either in a previous rule or within the same
+ * one.
+ * <programlisting>
+ * &ast; {
+ *     engine: someengine;
+ *     -SomeEngine-custom-property: 2;
+ * }
+ * </programlisting>
+ * </note>
+ *
+ * Since: 3.0
+ **/
+void
+gtk_theming_engine_register_property (const gchar            *name_space,
+                                      GtkStylePropertyParser  parse_func,
+                                      GParamSpec             *pspec)
+{
+  GtkStyleProperty *node;
+  GValue initial = { 0, };
+  gchar *name;
+
+  g_return_if_fail (name_space != NULL);
+  g_return_if_fail (strchr (name_space, ' ') == NULL);
+  g_return_if_fail (G_IS_PARAM_SPEC (pspec));
+
+  name = g_strdup_printf ("-%s-%s", name_space, pspec->name);
+  gtk_css_custom_property_create_initial_value (pspec, &initial);
+
+  node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
+                       "initial-value", &initial,
+                       "name", name,
+                       "value-type", pspec->value_type,
+                       NULL);
+  node->pspec = pspec;
+  node->property_parse_func = parse_func;
+
+  g_value_unset (&initial);
+  g_free (name);
+}
+
+/**
+ * gtk_style_properties_register_property: (skip)
+ * @parse_func: parsing function to use, or %NULL
+ * @pspec: the #GParamSpec for the new property
+ *
+ * Registers a property so it can be used in the CSS file format.
+ * This function is the low-level equivalent of
+ * gtk_theming_engine_register_property(), if you are implementing
+ * a theming engine, you want to use that function instead.
+ *
+ * Since: 3.0
+ **/
+void
+gtk_style_properties_register_property (GtkStylePropertyParser  parse_func,
+                                        GParamSpec             *pspec)
+{
+  GtkStyleProperty *node;
+  GValue initial = { 0, };
+
+  g_return_if_fail (G_IS_PARAM_SPEC (pspec));
+
+  gtk_css_custom_property_create_initial_value (pspec, &initial);
+
+  node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
+                       "initial-value", &initial,
+                       "name", pspec->name,
+                       "value-type", pspec->value_type,
+                       NULL);
+  node->pspec = pspec;
+  node->property_parse_func = parse_func;
+
+  g_value_unset (&initial);
+}
+
+/**
+ * gtk_style_properties_lookup_property: (skip)
+ * @property_name: property name to look up
+ * @parse_func: (out): return location for the parse function
+ * @pspec: (out) (transfer none): return location for the #GParamSpec
+ *
+ * Returns %TRUE if a property has been registered, if @pspec or
+ * @parse_func are not %NULL, the #GParamSpec and parsing function
+ * will be respectively returned.
+ *
+ * Returns: %TRUE if the property is registered, %FALSE otherwise
+ *
+ * Since: 3.0
+ **/
+gboolean
+gtk_style_properties_lookup_property (const gchar             *property_name,
+                                      GtkStylePropertyParser  *parse_func,
+                                      GParamSpec             **pspec)
+{
+  GtkStyleProperty *node;
+  gboolean found = FALSE;
+
+  g_return_val_if_fail (property_name != NULL, FALSE);
+
+  node = _gtk_style_property_lookup (property_name);
+
+  if (node)
+    {
+      if (pspec)
+        *pspec = node->pspec;
+
+      if (parse_func)
+        *parse_func = node->property_parse_func;
+
+      found = TRUE;
+    }
+
+  return found;
+}
+
diff --git a/gtk/gtkcsscustompropertyprivate.h b/gtk/gtkcsscustompropertyprivate.h
new file mode 100644 (file)
index 0000000..483dd6d
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Copyright © 2011 Red Hat Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors: Benjamin Otte <otte@gnome.org>
+ */
+
+#ifndef __GTK_CSS_CUSTOM_PROPERTY_PRIVATE_H__
+#define __GTK_CSS_CUSTOM_PROPERTY_PRIVATE_H__
+
+#include "gtk/gtkcssstylepropertyprivate.h"
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_CSS_CUSTOM_PROPERTY           (_gtk_css_custom_property_get_type ())
+#define GTK_CSS_CUSTOM_PROPERTY(obj)           (G_TYPE_CHECK_INSTANCE_CAST (obj, GTK_TYPE_CSS_CUSTOM_PROPERTY, GtkCssCustomProperty))
+#define GTK_CSS_CUSTOM_PROPERTY_CLASS(cls)     (G_TYPE_CHECK_CLASS_CAST (cls, GTK_TYPE_CSS_CUSTOM_PROPERTY, GtkCssCustomPropertyClass))
+#define GTK_IS_CSS_CUSTOM_PROPERTY(obj)        (G_TYPE_CHECK_INSTANCE_TYPE (obj, GTK_TYPE_CSS_CUSTOM_PROPERTY))
+#define GTK_IS_CSS_CUSTOM_PROPERTY_CLASS(obj)  (G_TYPE_CHECK_CLASS_TYPE (obj, GTK_TYPE_CSS_CUSTOM_PROPERTY))
+#define GTK_CSS_CUSTOM_PROPERTY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_CSS_CUSTOM_PROPERTY, GtkCssCustomPropertyClass))
+
+typedef struct _GtkCssCustomProperty           GtkCssCustomProperty;
+typedef struct _GtkCssCustomPropertyClass      GtkCssCustomPropertyClass;
+
+struct _GtkCssCustomProperty
+{
+  GtkCssStyleProperty parent;
+};
+
+struct _GtkCssCustomPropertyClass
+{
+  GtkCssStylePropertyClass parent_class;
+};
+
+GType                   _gtk_css_custom_property_get_type        (void) G_GNUC_CONST;
+
+
+G_END_DECLS
+
+#endif /* __GTK_CSS_CUSTOM_PROPERTY_PRIVATE_H__ */
index 9d26f3a1178ee021120c9802bd70dcdabe91840b..bef1fbfc487d1d0e366536d8abb9ecb8d82dff4a 100644 (file)
@@ -352,74 +352,6 @@ gtk_style_properties_provider_private_init (GtkStyleProviderPrivateInterface *if
   iface->lookup = gtk_style_properties_provider_lookup;
 }
 
-/* Property registration functions */
-
-/**
- * gtk_style_properties_register_property: (skip)
- * @parse_func: parsing function to use, or %NULL
- * @pspec: the #GParamSpec for the new property
- *
- * Registers a property so it can be used in the CSS file format.
- * This function is the low-level equivalent of
- * gtk_theming_engine_register_property(), if you are implementing
- * a theming engine, you want to use that function instead.
- *
- * Since: 3.0
- **/
-void
-gtk_style_properties_register_property (GtkStylePropertyParser  parse_func,
-                                        GParamSpec             *pspec)
-{
-  g_return_if_fail (G_IS_PARAM_SPEC (pspec));
-
-  _gtk_style_property_register (pspec,
-                                0,
-                                parse_func,
-                                NULL,
-                                NULL,
-                                NULL);
-}
-
-/**
- * gtk_style_properties_lookup_property: (skip)
- * @property_name: property name to look up
- * @parse_func: (out): return location for the parse function
- * @pspec: (out) (transfer none): return location for the #GParamSpec
- *
- * Returns %TRUE if a property has been registered, if @pspec or
- * @parse_func are not %NULL, the #GParamSpec and parsing function
- * will be respectively returned.
- *
- * Returns: %TRUE if the property is registered, %FALSE otherwise
- *
- * Since: 3.0
- **/
-gboolean
-gtk_style_properties_lookup_property (const gchar             *property_name,
-                                      GtkStylePropertyParser  *parse_func,
-                                      GParamSpec             **pspec)
-{
-  GtkStyleProperty *node;
-  gboolean found = FALSE;
-
-  g_return_val_if_fail (property_name != NULL, FALSE);
-
-  node = _gtk_style_property_lookup (property_name);
-
-  if (node)
-    {
-      if (pspec)
-        *pspec = node->pspec;
-
-      if (parse_func)
-        *parse_func = node->property_parse_func;
-
-      found = TRUE;
-    }
-
-  return found;
-}
-
 /* GtkStyleProperties methods */
 
 /**
index e6e84f4df248dd3e0a57547c6af0a238f0efe6f1..a2398bd15217314fedaa115f0202ef450b94ec33 100644 (file)
@@ -67,7 +67,7 @@ typedef gboolean (* GtkStylePropertyParser) (const gchar  *string,
 
 GType gtk_style_properties_get_type (void) G_GNUC_CONST;
 
-/* Functions to register style properties */
+/* Next 2 are implemented in gtkcsscustomproperty.c */
 void     gtk_style_properties_register_property (GtkStylePropertyParser  parse_func,
                                                  GParamSpec             *pspec);
 gboolean gtk_style_properties_lookup_property   (const gchar             *property_name,
index 05795152eba2857c2a0244a5178d85064c23e0ba..2eaea34fbb7893d9f477d7a34a59e8107e34cdda 100644 (file)
@@ -338,65 +338,6 @@ _gtk_theming_engine_set_context (GtkThemingEngine *engine,
   priv->context = context;
 }
 
-/**
- * gtk_theming_engine_register_property: (skip)
- * @name_space: namespace for the property name
- * @parse_func: parsing function to use, or %NULL
- * @pspec: the #GParamSpec for the new property
- *
- * Registers a property so it can be used in the CSS file format,
- * on the CSS file the property will look like
- * "-${@name_space}-${property_name}". being
- * ${property_name} the given to @pspec. @name_space will usually
- * be the theme engine name.
- *
- * For any type a @parse_func may be provided, being this function
- * used for turning any property value (between ':' and ';') in
- * CSS to the #GValue needed. For basic types there is already
- * builtin parsing support, so %NULL may be provided for these
- * cases.
- *
- * <note>
- * Engines must ensure property registration happens exactly once,
- * usually GTK+ deals with theming engines as singletons, so this
- * should be guaranteed to happen once, but bear this in mind
- * when creating #GtkThemeEngine<!-- -->s yourself.
- * </note>
- *
- * <note>
- * In order to make use of the custom registered properties in
- * the CSS file, make sure the engine is loaded first by specifying
- * the engine property, either in a previous rule or within the same
- * one.
- * <programlisting>
- * &ast; {
- *     engine: someengine;
- *     -SomeEngine-custom-property: 2;
- * }
- * </programlisting>
- * </note>
- *
- * Since: 3.0
- **/
-void
-gtk_theming_engine_register_property (const gchar            *name_space,
-                                      GtkStylePropertyParser  parse_func,
-                                      GParamSpec             *pspec)
-{
-  gchar *name;
-
-  g_return_if_fail (name_space != NULL);
-  g_return_if_fail (strchr (name_space, ' ') == NULL);
-  g_return_if_fail (G_IS_PARAM_SPEC (pspec));
-
-  /* FIXME: hack hack hack, replacing pspec->name to include namespace */
-  name = g_strdup_printf ("-%s-%s", name_space, pspec->name);
-  pspec->name = (char *)g_intern_string (name);
-  g_free (name);
-
-  gtk_style_properties_register_property (parse_func, pspec);
-}
-
 /**
  * gtk_theming_engine_get_property:
  * @engine: a #GtkThemingEngine
index d19d6d3b7f1b0b2cba0a8d7746f328116f28bccf..d3f6c83cb7489636b7bc574d71ce114ffb97e8d6 100644 (file)
@@ -185,6 +185,7 @@ GType gtk_theming_engine_get_type (void) G_GNUC_CONST;
 void _gtk_theming_engine_set_context (GtkThemingEngine *engine,
                                       GtkStyleContext  *context);
 
+/* function implemented in gtkcsscustomproperty.c */
 void gtk_theming_engine_register_property (const gchar            *name_space,
                                            GtkStylePropertyParser  parse_func,
                                            GParamSpec             *pspec);